package com.hz.web.controller.front;

import com.hz.web.entity.CkHtmlEntity;
import com.hz.web.entity.vo.IndexNewsVo;
import com.hz.web.entity.vo.IndexResponseVo;
import com.hz.web.entity.vo.ListResponseVo;
import com.hz.web.service.IckHtmlService;
import com.hz.web.util.DateUtil;
import org.jsoup.helper.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("front")
public class ListController extends BasicController {

    @Resource
    private IckHtmlService ckHtmlService;


    /**
     * 相关的文章列表页
     * @param session
     * @param inputVal 搜索关键字
     * @param ckType
     * @param curpage
     * @param size
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public ListResponseVo index(HttpSession session, String inputVal, String ckType, Integer curpage, Integer size) {
        ListResponseVo listResponseVo = new ListResponseVo();

        Map<String, Object> parmsMap = new HashMap<String, Object>();// 参数map
        if (curpage != 0 && size != 0) {
            int index = (curpage - 1) * size;
            int end = size;

            parmsMap.put("index", index);
            parmsMap.put("end", end);
        }

        if(StringUtil.isBlank(ckType) && !StringUtil.isBlank(inputVal.trim())){ // 首页搜索
            parmsMap.put("inputVal", "%" + inputVal.trim() + "%");
        }else{
            parmsMap.put("ckType", ckType);// 富文本类型 判断是哪个功能的富文本信息
        }

        parmsMap.put("state", "1");
        //parmsMap.put("imgpd", "a"); // 不为空即可
        parmsMap.put("sort", "yes");

        List<IndexNewsVo> newsList = new ArrayList<>();

        List<CkHtmlEntity> ckHtmlEntityList = ckHtmlService.queryCkHtml(parmsMap);

        if(ckHtmlEntityList == null || ckHtmlEntityList.isEmpty()){ // 没查到数据，直接返回
            listResponseVo.setResult(newsList);
            listResponseVo.setTotal(0);
            listResponseVo.setSize(size);
            listResponseVo.setCurpage(curpage);
            return listResponseVo;
        }

        for (CkHtmlEntity ckHtmlEntity : ckHtmlEntityList) {
            IndexNewsVo newsVo = new IndexNewsVo();
            newsVo.setTitle(ckHtmlEntity.getTitle());
            newsVo.setLink(host + "/detail.jsp?ckType=" + ckHtmlEntity.getCkType() + "&id=" + ckHtmlEntity.getId());
            newsVo.setCreatedate(DateUtil.dateToStr(ckHtmlEntity.getCreateTime(), "yyyy-MM-dd"));
            newsList.add(newsVo);
        }

        int total = ckHtmlService.queryCkHtmlCount(parmsMap);

        listResponseVo.setResult(newsList);
        listResponseVo.setTotal(total);
        listResponseVo.setSize(size);
        listResponseVo.setCurpage(curpage);
        return listResponseVo;
    }


    /**
     * 列表页的左侧热点
     * @param session
     * @return
     */
    @RequestMapping("hot")
    @ResponseBody
    public IndexResponseVo index(HttpSession session) {

        IndexResponseVo indexResponseVo = new IndexResponseVo();
        LinkedHashMap<String, List<IndexNewsVo>> result = new LinkedHashMap<>();

        // 新闻热点
        List<IndexNewsVo> newsList = new ArrayList<>();

        Map<String, Object> parmsMapForNews = new HashMap<String, Object>();// 参数map
        parmsMapForNews.put("ckType", 8);// 富文本类型 判断是哪个功能的富文本信息
        parmsMapForNews.put("state", "1");
        //parmsMapForNews.put("imgpd", "a"); // 不为空即可
        parmsMapForNews.put("sort", "yes");
        parmsMapForNews.put("index", 0);
        parmsMapForNews.put("end", 10);
        List<CkHtmlEntity> ckHtmlEntityListForNews = ckHtmlService.queryCkHtml(parmsMapForNews);

        for(CkHtmlEntity ckHtmlEntity: ckHtmlEntityListForNews){
            //String context = ckHtmlEntity.getHtml().substring(0, 200).replaceAll("</?[^>]+>", "").replaceAll("<a>\\s*|t|r|n</a>", "").replaceAll("&nbsp;", "");
            IndexNewsVo newsVo = new IndexNewsVo();
            newsVo.setTitle(ckHtmlEntity.getTitle());
            //newsVo.setIntro(context.length()> 30 ? context.substring(0,30) : context);
            newsVo.setLink(host + "/detail.jsp?parent=7&menu=18&ckType=8&id=" + ckHtmlEntity.getId());
            newsVo.setCreatedate(DateUtil.dateToStr(ckHtmlEntity.getCreateTime(), "MM-dd"));
            newsList.add(newsVo);
        }
        result.put("news", newsList);


        // 通知公告
        List<IndexNewsVo> announceList = new ArrayList<>();

        Map<String, Object> parmsMapForAnnounce = new HashMap<String, Object>();// 参数map
        parmsMapForAnnounce.put("ckType", 11);// 富文本类型 判断是哪个功能的富文本信息
        parmsMapForAnnounce.put("state", "1");
        //parmsMapForAnnounce.put("imgpd", "a"); // 不为空即可
        parmsMapForAnnounce.put("sort", "yes");
        parmsMapForAnnounce.put("index", 0);
        parmsMapForAnnounce.put("end", 10);
        List<CkHtmlEntity> ckHtmlEntityListForAnnounce = ckHtmlService.queryCkHtml(parmsMapForAnnounce);

        for(CkHtmlEntity ckHtmlEntity: ckHtmlEntityListForAnnounce){
            IndexNewsVo newsVo = new IndexNewsVo();
            newsVo.setTitle(ckHtmlEntity.getTitle());
            newsVo.setLink(host + "/detail.jsp?parent=7&menu=24&ckType=11&id=" + ckHtmlEntity.getId());
            newsVo.setCreatedate(DateUtil.dateToStr(ckHtmlEntity.getCreateTime(), "MM-dd"));
            announceList.add(newsVo);
        }
        result.put("subject", announceList);


        // 返回组装的信息
        indexResponseVo.setResult(result);
        System.out.println(indexResponseVo);
        return indexResponseVo;
    }

}
